﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Infrastructure.Utility
{
   public class Reflector
   {
      public static T CreateInstance<T>(string assemblyName, string className,string nameSpace) where T : class
      {
         string[] arr=className.Split('.');
         className = (string.IsNullOrEmpty(nameSpace) ? assemblyName : nameSpace) +"."+ arr[arr.Length - 1];
         return System.Reflection.Assembly.Load(assemblyName).CreateInstance(className) as T;
      }

      public static object CreateInstance(string assemblyName, string className, string nameSpace)
      {
         string[] arr = className.Split('.');
         className = (string.IsNullOrEmpty(nameSpace) ? assemblyName : nameSpace) + "." + arr[arr.Length - 1];
         return Assembly.Load(assemblyName).CreateInstance(className);
      }

      public static object MethodInvoke(object obj, string methodName,params object[] args)
      {
         Type type = obj.GetType();
         MethodInfo methodInfo = type.GetMethod(methodName);
         return methodInfo.Invoke(obj, args);
      }

      public static void SetPropertyValue(object obj, string propertyName, object value)
      {
         Type type = obj.GetType();
         type.GetProperty(propertyName).SetValue(obj, value, null);
      }
      public static object GetPropertyValue(object obj, string propertyName)
      {
          Type type = obj.GetType();
          return type.GetProperty(propertyName).GetValue(obj, null);
      }
   }
}
